home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / getgrent.cc < prev    next >
C/C++ Source or Header  |  1996-11-20  |  4KB  |  235 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <string>
  28.  
  29. #ifdef HAVE_SYS_TYPES_H
  30. #include <sys/types.h>
  31. #endif
  32.  
  33. #ifdef HAVE_GRP_H
  34. #include <grp.h>
  35. #endif
  36.  
  37. #include "defun-dld.h"
  38. #include "error.h"
  39. #include "gripes.h"
  40. #include "help.h"
  41. #include "oct-map.h"
  42. #include "ov.h"
  43. #include "oct-obj.h"
  44. #include "utils.h"
  45.  
  46. // Group file functions.  (Why not?)
  47.  
  48. static octave_value
  49. mk_gr_map (struct group *gr)
  50. {
  51.   octave_value retval;
  52.  
  53.   if (gr)
  54.     {
  55.       Octave_map m;
  56.  
  57.       m ["name"] = gr->gr_name;
  58. #if defined (HAVE_GR_PASSWD)
  59.       m ["passwd"] = gr->gr_passwd;
  60. #else
  61.       m ["passwd"] = "";
  62. #endif
  63.       m ["gid"] = STATIC_CAST (double, gr->gr_gid);
  64.  
  65.       if (gr->gr_mem)
  66.     {
  67.       // XXX FIXME XXX -- maybe there should be a string_vector
  68.       // constructor that takes a NULL terminated list of C
  69.       // strings.
  70.  
  71.       char **tmp = gr->gr_mem;
  72.  
  73.       int k = 0;
  74.       while (*tmp++)
  75.         k++;
  76.  
  77.       if (k > 0)
  78.         {
  79.           tmp = gr->gr_mem;
  80.  
  81.           string_vector members (k);
  82.  
  83.           for (int i = 0; i < k; i++)
  84.         members[i] = tmp[i];
  85.  
  86.           m ["mem"] = members;
  87.         }
  88.       else
  89.         m ["mem"] = "";
  90.     }
  91.  
  92.       retval = m;
  93.     }
  94.   else
  95.     retval = 0.0;
  96.  
  97.   return retval;
  98. }
  99.  
  100. DEFUN_DLD (getgrent, args, ,
  101.  "getgrent ()\n\
  102. \n\
  103. Read an entry from the group-file stream, opening it if necessary.")
  104. {
  105.   octave_value retval;
  106.  
  107.   int nargin = args.length ();
  108.  
  109.   if (nargin == 0)
  110.     {
  111. #ifdef HAVE_GETGRENT
  112.       retval = mk_gr_map (getgrent ());
  113. #else
  114.       gripe_not_supported ("getgrent");
  115. #endif
  116.     }
  117.   else
  118.     print_usage ("getgrent");
  119.  
  120.   return retval;
  121. }
  122.  
  123. DEFUN_DLD (getgrgid, args, ,
  124.   "getgrgid (GID)\n\
  125. \n\
  126. Search for a group entry with a matching group ID.")
  127. {
  128.   octave_value retval;
  129.  
  130.   int nargin = args.length ();
  131.  
  132.   if (nargin == 1)
  133.     {
  134. #ifdef HAVE_GETGRGID
  135.       double dval = args(0).double_value ();
  136.  
  137.       if (! error_state)
  138.     {
  139.       if (D_NINT (dval) == dval)
  140.         {
  141.           gid_t gid = STATIC_CAST (gid_t, dval);
  142.  
  143.           retval = mk_gr_map (getgrgid (gid));
  144.         }
  145.       else
  146.         error ("getgrgid: argument must be an integer");
  147.     }
  148. #else
  149.       gripe_not_supported ("getgrgid");
  150. #endif
  151.     }
  152.   else
  153.     print_usage ("getgrgid");
  154.  
  155.   return retval;
  156. }
  157.  
  158. DEFUN_DLD (getgrnam, args, ,
  159.   "getgrnam (NAME)\n\
  160. \n\
  161. Search for group entry with a matching group name.")
  162. {
  163.   octave_value retval;
  164.  
  165.   int nargin = args.length ();
  166.  
  167.   if (nargin == 1)
  168.     {
  169. #ifdef HAVE_GETGRNAM
  170.       string s = args(0).string_value ();
  171.  
  172.       if (! error_state)
  173.     retval = mk_gr_map (getgrnam (s.c_str ()));
  174. #else
  175.       gripe_not_supported ("getgrnam");
  176. #endif
  177.     }
  178.   else
  179.     print_usage ("getgrnam");
  180.  
  181.   return retval;
  182. }
  183.  
  184. DEFUN_DLD (setgrent, args, ,
  185.   "setgrent ()\n\
  186. \n\
  187. Rewind the group-file stream.")
  188. {
  189.   octave_value retval;
  190.  
  191.   int nargin = args.length ();
  192.  
  193.   if (nargin == 0)
  194.     {
  195. #ifdef HAVE_SETGRENT
  196.       setgrent ();
  197. #else
  198.       gripe_not_supported ("setgrent");
  199. #endif
  200.     }
  201.   else
  202.     print_usage ("setgrent");
  203.  
  204.   return retval;
  205. }
  206.  
  207. DEFUN_DLD (endgrent, args, ,
  208.   "endgrent ()\n\
  209. \n\
  210. Close the group-file stream.")
  211. {
  212.   octave_value retval;
  213.  
  214.   int nargin = args.length ();
  215.  
  216.   if (nargin == 0)
  217.     {
  218. #ifdef HAVE_ENDGRENT
  219.       endgrent ();
  220. #else
  221.       gripe_not_supported ("endgrent");
  222. #endif
  223.     }
  224.   else
  225.     print_usage ("endgrent");
  226.  
  227.   return retval;
  228. }
  229.  
  230. /*
  231. ;;; Local Variables: ***
  232. ;;; mode: C++ ***
  233. ;;; End: ***
  234. */
  235.